package day02;

import util.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class TestBatch {

    public static void main(String[] args) {

        Connection con=null;
        PreparedStatement ps=null;
        try{
            con= JdbcUtil.getConnection();

            con.setAutoCommit(false);


            String sql=new StringBuffer()
                    .append("insert into t_user ")
                    .append("    (name) ")
                    .append("values ")
                    .append("    (?) ")
                    .toString();

            ps=con.prepareStatement(sql);



            long start=System.currentTimeMillis();
//            for (int i = 0; i < 290000; i++) {
//                ps.setString(1,"u"+i);
//                ps.executeUpdate();//17448
//            }


            for (int i = 0; i < 290001; i++) {
                ps.setString(1,"u"+i);
                ps.addBatch();

                if(i%10000==0){
                    ps.executeBatch();//16383
                }
            }

            ps.executeBatch();
            
            
            
            
            
            long end=System.currentTimeMillis();

            System.out.println(end-start);












            con.commit();
        }catch (Exception e){
            e.printStackTrace();
            try {
                con.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }finally{
            JdbcUtil.close(null,ps,con);
        }




    }



}
